package com.yunkeji.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.vo.SysCityVo;
import com.yunkeji.system.domain.bo.SysCityBo;
import com.yunkeji.system.service.ISysCityService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 城市信息
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysCity")
public class SysCityController extends BaseController {

    private final ISysCityService iSysCityService;

    /** 查询城市信息列表 */
    @SaCheckPermission("system:sysCity:list")
    @GetMapping("/list")
    public TableDataInfo<SysCityVo> list(SysCityBo bo, PageQuery pageQuery) {
        return iSysCityService.queryPageList(bo, pageQuery);
    }

    /** 导出城市信息列表 */
    @SaCheckPermission("system:sysCity:export")
    @Log(title = "城市信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysCityBo bo, HttpServletResponse response) {
        List<SysCityVo> list = iSysCityService.queryList(bo);
        ExcelUtil.exportExcel(list, "城市信息", SysCityVo.class, response);
    }

    /**
     * 获取城市信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:sysCity:query")
    @GetMapping("/{id}")
    public R<SysCityVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSysCityService.queryById(id));
    }

    /** 新增城市信息 */
    @SaCheckPermission("system:sysCity:add")
    @Log(title = "城市信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysCityBo bo) {
        return toAjax(iSysCityService.insertByBo(bo));
    }

    /** 修改城市信息 */
    @SaCheckPermission("system:sysCity:edit")
    @Log(title = "城市信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysCityBo bo) {
        return toAjax(iSysCityService.updateByBo(bo));
    }

    /**
     * 删除城市信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:sysCity:remove")
    @Log(title = "城市信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysCityService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
